import os
import shutil
import re

# PART 1 ======================================================
# List of folder and corresponding simulation parameters
folder_list = [('m2_0.01',0.01),
               ('m2_0.1',0.1),
               ('m2_1.0',1.0)]
# Constants that are not changed in this set of simulations
r2_set = 170
q_r2 = 0.0
q_m2 = 1.0
time = 100
dt = 1.0
repeat = 1

# PART 2 =======================================================
# The following part usually don't need any change
print "making the following folder(s) "
for i in range(len(folder_list)):
    folder=folder_list[i][0]
    m2_set=folder_list[i][1]
    print i, folder
    if os.path.exists(folder):
        pass
    else:
        os.mkdir(folder)
#    shutil.copy('./simulation.template',folder+'/simulation.sh')
    p_folder = re.compile('VAL_CUR_FOLDER')
    p_r2_set = re.compile('VAL_R2_SET')
    p_q_r2   = re.compile('VAL_Q_R2')
    p_m2_set = re.compile('VAL_M2_SET')
    p_q_m2   = re.compile('VAL_Q_M2')
    p_repeat = re.compile('VAL_REPEAT')
    p_dt     = re.compile('VAL_DT')
    p_t_end  = re.compile('VAL_T_END')
    if 'nt'==os.name:
        fi = open('../simulation.template.bat')
        fo = open(folder+'/simulation.bat','w')
    else:
        fi = open('../simulation.template.sh')
        fo = open(folder+'/simulation.sh','w')
    for line in fi:
        line = p_folder.sub(folder,line)
        line = p_r2_set.sub(str(r2_set),line)
        line = p_q_r2.sub(str(q_r2),line)
        line = p_m2_set.sub(str(m2_set),line)
        line = p_q_m2.sub(str(q_m2),line)
        line = p_repeat.sub(str(repeat),line)
        line = p_dt.sub(str(dt),line)
        line = p_t_end.sub(str(time),line)
        fo.write(line)
    fi.close()
    fo.close()
    # Submit jobs using qsub
#    if 'posix'==os.name:
#        echo "Windows"
    
#        os.system('qsub -v LD_LIBRARY_PATH -l h_rt=12:00:00 '+folder+'/simulation.sh')
#        command='qsub -v LD_LIBRARY_PATH -l h_rt=12:00:00 '+folder+'/simulation.sh'
#       os.system('echo '+command)
 
    
        
    
    

    

